﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using NLBS.Framework;
using NLBS.Entity;
using NLBS.Web;
using NLBS.Data.Business;
using NLBS.Data;
using System.Web;

namespace NLBS.Control
{
    public class PanelTag : PanelModule
    {
        [TemplateContainer(typeof(DefaultTagTemplate))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public override ITemplate ItemTemplate
        {
            get
            {
                return base.ItemTemplate;
            }
            set
            {
                base.ItemTemplate = value;
            }
        }

        public override string Title
        {
            get
            {
                return "标签";
            }
            set
            {
                base.Title = value;
            }
        }

        protected override void CreateChildControls()
        {
            if (ItemTemplate == null)
                ItemTemplate = new DefaultTagTemplate();
            base.CreateChildControls();
        }

    }

    public class DefaultTagTemplate : ITemplate
    {


        private IDictionary<string, string> FormatTag()
        {
            ITagCollection<string, Tag> tagDic = DbProvider.Instance().GetTags();
            int max = tagDic.MaxItem;
            IDictionary<string, string> tagList = new Dictionary<string, string>();
           // IList<Tag> tagList = new List<Tag>();
            foreach (string key in tagDic.Keys)
            {

                double weight = ((double)tagDic[key].Count / max) * 100;
                if (weight >= 99)
                    tagList.Add(key, "biggest");
                else if (weight >= 70)
                    tagList.Add(key, "big");
                else if (weight >= 40)
                    tagList.Add(key, "medium");
                else if (weight >= 20)
                    tagList.Add(key, "small");
                else if (weight >= 3)
                    tagList.Add(key, "smallest");
            }
            return tagList;
        }


        public void InstantiateIn(System.Web.UI.Control container)
        {
            container.Controls.Add(new LiteralControl("<ul>"));
            IDictionary<string, string> tagList = FormatTag();
            string LINK = "<a href=\"{0}\" class=\"{1}\" title=\"{2}\">{3} </a>";
            if (tagList != null)
            {
                foreach (string key in tagList.Keys)
                {
                    container.Controls.Add(new LiteralControl("<li>"));
                    container.Controls.Add(new LiteralControl(string.Format(LINK,"javascript:NLBSPage.ArticleCall(1,'tag','"+HttpUtility.UrlEncode(key)+"')", tagList[key], "Tag: " + key, key)));
                    container.Controls.Add(new LiteralControl("</li>"));
                }
            }
            container.Controls.Add(new LiteralControl("</ul>"));
        }


    }
}